package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.config.Result;
import com.atguigu.springcloud.service.IMoiveTikectService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 购票信息 前端控制器
 */
@Slf4j
@Api(value = "购票信息管理", tags = "购票信息管理模块")
@RestController
@RequestMapping("/buyticket")
@DefaultProperties(defaultFallback = "global_FallbackMethod")
public class MovieTicketInfoController {

  @Autowired
  private IMoiveTikectService iMoiveTikectService;

  @PostMapping("/buy")
  @ApiOperation("购票")
  @ApiImplicitParams({
          @ApiImplicitParam(value = "购买张数", name = "number", dataType = "Integer"),
          @ApiImplicitParam(value = "电影ID", name = "movieId", dataType = "Integer"),
          @ApiImplicitParam(value = "用户ID", name = "userId", dataType = "Integer")
  })
  @HystrixCommand(fallbackMethod = "global_FallbackMethod",commandProperties = {
          @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//是否开启断路器
          @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//请求次数
          @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),//时间窗口期
          @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")//失败率达到多少后跳闸 //6次
  })
  public Result<String> bugTicket(Integer number, Integer movieId, Integer userId) throws IllegalAccessException {
    return iMoiveTikectService.bugTicket(number, movieId, userId);
  }

  //下面全局fallback方法
  public Result<String> global_FallbackMethod(Integer number, Integer movieId, Integer userId) {
    log.error("请检查你的传入参数，网络情况是否良好, number：" + number + " movieId：" + movieId + " userId：" + userId);
    return Result.fail("请检查你的传入参数，网络情况是否良好, number：" + number + " movieId：" + movieId + " userId：" + userId);
  }
}
